package com.itboye.shop.me.adress;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.adapter.CityAdapter;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.CitysBean;

import java.util.ArrayList;

/**
 * 市
 */
public class CityActivity extends BaseActivity {
    TextView tvTitle;
    ImageView back;

    private ListView city_listview;
    private SQLiteDatabase database;
    private ArrayList<CitysBean> litCity;// 区域列表
    private CityAdapter adapter;
    private CityDBManager cityDBManager;
    private String provinceId;//上个页面传过来的省id
    private String provinceName;//上个页面传过来的省name

    private ArrayList<CitysBean> litArae;// 区域列表
    String cityId;
    String cityName;
    String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        tvTitle.setText("选择地址");

        provinceId=getIntent().getStringExtra("provinceId");
        provinceName=getIntent().getStringExtra("provinceName");
        type=this.getIntent().getStringExtra("type");
        cityDBManager = new CityDBManager(this);
        cityDBManager.openDateBase();
        database = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,null);

        litCity = cityDBManager.queryCity(provinceId);
        adapter=new CityAdapter(this,litCity);
        city_listview.setAdapter(adapter);

        city_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityId=litCity.get(position).getCityId();
                cityName=litCity.get(position).getCityName();

                litArae = cityDBManager.queryArea(cityId);
                if(litArae.size()==0||litArae==null){
                    Intent intent=new Intent(CityActivity.this,AddressDetailActivity.class);
                    intent.putExtra("provinceName",provinceName);//添加要返回给页面1的数据
                    intent.putExtra("cityName",cityName.equals("全部")||cityName.equals("不限")?"":cityName);//添加要返回给页面1的数据
                    intent.putExtra("type",type);
                    intent.putExtra("name",getIntent().getStringExtra("name"));
                    intent.putExtra("mobile",getIntent().getStringExtra("mobile"));
                    intent.putExtra("youbian",getIntent().getStringExtra("youbian"));
                    intent.putExtra("street",getIntent().getStringExtra("street"));
                    intent.putExtra("default",getIntent().getStringExtra("default"));
                    startActivity(intent);

                    // 发送广播通知LoginActivity结束
                    Intent intent1 = new Intent(AddressDetailActivity.ADDRESSEXIT);
                    sendBroadcast(intent1);
                    finish();
                }else{
                    Intent intent=new Intent(CityActivity.this,AraeActivity.class);
                    intent.putExtra("cityId",cityId);
                    intent.putExtra("provinceName",provinceName);
                    intent.putExtra("cityName",cityName.equals("市辖区")?"":cityName);
                    intent.putExtra("type",type);
                    intent.putExtra("name",getIntent().getStringExtra("name"));
                    intent.putExtra("mobile",getIntent().getStringExtra("mobile"));
                    intent.putExtra("youbian",getIntent().getStringExtra("youbian"));
                    intent.putExtra("street",getIntent().getStringExtra("street"));
                    intent.putExtra("default",getIntent().getStringExtra("default"));
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }
}
